import pandas as pd
import numpy as np
import pickle
import os


# ['user_id', 'age', 'gender', 'creative_id', 'time', 'click_times']
click_uid_csv = "/home/datanfs/macong_data/tencent_data/train_preliminary/click_uid.csv"


# 测试cilick_uid.csv文件写入正常,能正常读取
def desc_basic_info():
    df_click_udi = pd.read_csv(click_uid_csv)

    print(df_click_udi.describe())
    print(df_click_udi.info())
    for index, row in df_click_udi.iterrows():
        cid = row["creative_id"]
        cid = eval(cid)
        print(type(cid), len(cid))
        if index > 20:
            break
    print("columns: ", df_click_udi.columns)


# 测试ad.csv
def get_adid_csv_type():
    aid_csv = "/home/datanfs/macong_data/tencent_data/train_preliminary/ad.csv"
    df_ad = pd.read_csv(aid_csv)
    df_ad.loc[df_ad["product_id"] == "\\N"] = 0
    df_ad.loc[df_ad["industry"] == "\\N"] = 0
    # df_ad["product_id"] = pd.to_numeric(df_ad["product_id"])
    # df_ad["industry"] = pd.to_numeric(df_ad["industry"])

    for index, row in df_ad.iterrows():
        cid = row["creative_id"]
        if type(cid) == str:
            print("is str")
            break


    # df_ad = df_ad.astype(np.int32)
    # for index, row in df_ad.iterrows():
    #     cid = row["creative_id"]
    #     print(cid)
    #     if 0 == cid:
    #         print("is equal")
    #     else:
    #         print("not equal")
    #     print(type(cid))
    #     break

def test_dict_key_type():
    print("### test pickle dict key type ###")
    h5_train_pos = "/home/datanfs/macong_data/tencent_data/train_preliminary/train_data/h5train2.pkl"
    aid_dict_pos = "/home/datanfs/macong_data/tencent_data/" \
                   "train_preliminary/train_data/h5aid.pkl"
    path1 = h5_train_pos
    path2 =aid_dict_pos

    f1 = open(path1, 'rb')
    f2 = open(path2, 'rb')

    data1 = pickle.load(f1)
    data2 = pickle.load(f2)

    print(type(data1["cnt2word"].keys()))
    print(type(data1["word2cnt"].keys()))

    print(type(data2["product_vocab_dict"].keys()))

    cnt_times = 0
    for key,value in data1["cnt2word"].items():
        print("cnt2word type", type(key), type(value))
        print(key, value)
        # if 648070 in data1["cnt2word"]:
        #     print("in cnt2word")
        # else:
        #     print("not in cnt2word")
        # break
        if cnt_times > 10:
            break
        cnt_times += 1

    uid_str = data1["cnt2word"][648070]
    print("uid str", uid_str, type(uid_str))

    for key, value in data2["product_vocab_dict"].items():
        print(type(key), type(value))
        if uid_str in data2["product_vocab_dict"]:
            print("in product_vocab_dict")
        else:
            print("not in product_vocab_dict")
        break
    print("over")

if __name__ == '__main__':
    # desc_basic_info()
    # get_adid_csv_type()
    test_dict_key_type()
